﻿$(function(){//延迟加载
	var dvId=$('#dvId').val();
	var parentId=$('#parentId').val();
	var parentbelong=$('#parentbelong').val();
	var pbelong='';
	var pip='';
	var url='';
	if(parentId==''){
		url='showdvparent?dvId='+dvId;
	}else{
		url='showdvparent?dvId='+dvId+'&parentId='+parentId;
	}
	$('#b1').linkbutton({
		iconCls:'icon-add',
		plain:true,
		onClick:function(){
			append();
		}
	});
	$('#b2').linkbutton({
		iconCls:'icon-edit',
		plain:true,
		onClick:function(){
		removeit();
		}
	});
	$('#b3').linkbutton({
		iconCls:'icon-save',
		plain:true,
		onClick:function(){
		accept();
		}
	});
	$('#b4').linkbutton({
		iconCls:'icon-remove',
		plain:true,
		onClick:function(){
		reject();
		}
	});
	$('#mresult').combobox({
		disabled:true
	});
	$('#ssd').searchbox({
		searcher:function(value,name){
			pbelong=name;
			pip=value;
			if(value==''||name=='0'){
				alert('未输入');
			}else{
				$('#mresult').combobox({
					disabled:false,
					value:'请选择',
					disabled:false,
					valueField: 'parentId',    
			        textField: 'parentname',
					url:"querybyip?dvbelong="+name+"&dvip="+value,
				});
			}
		}, 
		menu:'#mm', 
		prompt:'设备ip..' 					
	}); 
	function showdp(){
		$('#dg').datagrid({
			iconCls: 'icon-edit',
			singleSelect: true,
			toolbar: '#tb',
			url: 'datagrid_data1.json',
			method: 'get',
			onClickRow: onClickRow
		});
	}
	
	$('#dg').datagrid({
		iconCls: 'icon-edit',
		singleSelect: true,
		toolbar:'#tb',
		url:url,
		method: 'get',
		onClickRow: onClickRow,
		columns:[[		          		       
		          {field:'dvname',width:'15%',title:'设备名称',editor:{
		        	  type:'textbox',
		        		 options:{
		        			 editable:false,
		        			 required:true 
		        		 }
		          }},
		          {field:'dvip',width:'15%',title:'设备IP',editor:{
		        	  type:'textbox',
		        		 options:{
		        			 editable:false,
		        			 required:true 
		        		 }
		          }},
		          {field:'dvId',width:0,title:'设备Id',editor:{
		        	  type:'textbox',
		        		 options:{
		        			 editable:false,
		        		 }
		          }},
		          {field:'dvslot',width:'15%',title:'设备端口',editor:{
		        	  type:'textbox',
		        		 options:{		        			 
		        			 required:true 
		        		 }
		          }},
		          {field:'parentname',width:'15%',title:'上联设备名称',editor:{
		        	  ype:'textbox',
		        		 options:{
		        			 editable:false,
		        			 required:true 
		        		 }
		          }},
		          {field:'parentip',width:'15%',title:'上联设备IP',editor:{
		        	  ype:'textbox',
		        		 options:{
		        			 editable:false,
		        			 required:true 
		        		 }
		          }},
		          {field:'parentslot',width:'15%',title:'上联设备端口',editor:{
		        	  type:'textbox',
		        		 options:{
		        			 required:true 
		        		 }
		          }},
		          {field:'parentId',width:0,title:'上联设备Id',editor:{
		        	  type:'textbox',
		        		 options:{
		        			 editable:false,
		        		 }
		          }},
		          {field:'dvrelaId',width:0,title:'设备关系Id',editor:{
		        	  type:'textbox',
		        		 options:{
		        			 editable:false,
		        		 }
		          }},
		          {field:'dvlevel',width:0,title:'设备关系Id',editor:{
		        	  type:'numberbox',
		        		 options:{
		        			 editable:false,
		        		 }
		          }},
		          {field:'parentlevel',width:0,title:'设备关系Id',editor:{
		        	  type:'numberbox',
		        		 options:{
		        			 editable:false,
		        		 }
		          }},
		          {field:'type',title:'主用',width:'10%',align:'center',editor:{type:'checkbox',options:{on:'1',off:'0'}}}	 	          
		          ]],
		onLoadSuccess:function(){
			$(this).datagrid('hideColumn','parentId');
			$(this).datagrid('hideColumn','dvId');
			$(this).datagrid('hideColumn','dvrelaId');
			$(this).datagrid('hideColumn','dvlevel');
			$(this).datagrid('hideColumn','parentlevel');
			//这块把DVNAME以及DVBELONG等值传给前台
			var crows=$('#dg').datagrid('getRows');
			if(crows!=undefined&&crows!=''){
				crow=crows[crows.length-1];
				$('#dvname').val(crow.dvname);
				$('#comdvbelong').val(crow.dvbelong);
				$('#dvip').val(crow.dvip);
			}						
			/*var row = $('#dg').datagrid('getData').rows[0];
			var dvname=row.dvname;
			var dvip=row.dvip;
			var parentname=row.parentname;
			var parentip=row.parentip
			$('#dg').datagrid('columns');
			*/
		}	
	});
	$('.datagrid-view').eq(0).after('<div id="edbutton"><a id="comaddparent" style="width:80px"></a></div>');
	$('#comaddparent').linkbutton({
			text:'完成',
		iconCls:'icon-ok',
		onClick:function(){
			var dvparent=$('#dg').datagrid('getData');
			$.ajax({
				type:'POST',
				url:'editparent',
				data:{
					dvId:dvId,
					listparent:JSON.stringify(dvparent),
				},				
				success:function(data){
					alert(data.msg);
				}
			});
		}
	});

	$('#ceshi1').click(function(){
		alert('OK');
//		var rowss=$('#dg').datagrid('getRows');
//		alert('DVRELAID:'+rowss[rowss.length-1].dvrelaId+',DVID:'+rowss[rowss.length-1].dvId+',DVIP:'+rowss[rowss.length-1].dvip+',PARENTID:'+rowss[rowss.length-1].parentId+',DVBELONG:'+rowss[rowss.length-1].dvbelong+
//				',PARENTBELONG:'+rowss[rowss.length-1].parentbelong+',PARENTIP:'+rowss[rowss.length-1].parentip);
		parent.addTab('测试','http://133.68.12.55:8080/xasj/toaddolt','zzjg');
	});
	var editIndex = undefined;	
	function endEditing(){
		if (editIndex == undefined){return true}
		if ($('#dg').datagrid('validateRow', editIndex)){
			//var ed = $('#dg').datagrid('getEditor', {index:editIndex,field:'productid'});
			//var productname = $(ed.target).combobox('getText');
			//$('#dg').datagrid('getRows')[editIndex]['productname'] = productname;
			//$('#dg').datagrid('endEdit', editIndex);
			editIndex = undefined;
			return true;
		} else {
			return false;
		}
	}

	function onClickRow(index){
		if (editIndex!= index){
			if (endEditing()){
				$('#dg').datagrid('selectRow', index)
						.datagrid('beginEdit', index);
				editIndex = index;
			} else {
				$('#dg').datagrid('selectRow', editIndex);
			}
		}
	}

	function append(){
		if (endEditing()){
			var frows=$('#dg').datagrid('getSelected');
			if(frows==''||frows==null)
			alert('未选中行');
			$('#dg').datagrid('appendRow',
				{
				type:'P',
				dvname:frows.dvname,
				dvip:frows.dvip,
				dvbelong:frows.dvbelong,
				dvId:frows.dvId,
				parentname:frows.parentname,
				parentip:frows.parentip,
				parentbelong:frows.parentbelong,
				parentId:frows.parentId
				});
			editIndex = $('#dg').datagrid('getRows').length-1;
			$('#dg').datagrid('selectRow', editIndex)
					.datagrid('beginEdit', editIndex);
			}
		}
	
	$('#addnew').linkbutton({
		iconCls:'icon-search',
		text:'添加',
		onClick:function(){
				if (endEditing()){
					$('#dg').datagrid('appendRow',
						{
						type:'P',
						dvname:$('#dvname').val(),
						dvip:$('#dvip').val(),
						dvbelong:$('#comdvbelong').val(),
						dvId:$('#dvId').val().split(",")[0],
						parentname:$('#mresult').combobox('getText'),
						parentip:$('#mresult').combobox('getValue').split(",")[1],
						parentbelong:pbelong,
						parentId:$('#mresult').combobox('getValue').split(",")[0]
						});
					editIndex = $('#dg').datagrid('getRows').length-1;
					$('#dg').datagrid('selectRow', editIndex)
							.datagrid('beginEdit', editIndex);
					}

		}
	});
	
	function removeit(){
		if (editIndex == undefined){return}
		var drow=$('#dg').datagrid('getRows')[editIndex];
		//alert(drow.dvrelaId);
		if(drow.dvrelaId==undefined|| drow.dvrelaId==''){			
			return
		}
//		$.ajax({
//			type:'POST',
//			url:'deletedvrelarow',
//			data:{
//				dvslot:drow.dvslot,
//				parentslot:drow.parentslot,
//				dvrelaId:drow.dvrelaId
//			},
//			success:function(data){
//				$('#dg').datagrid('cancelEdit', editIndex)
//				.datagrid('deleteRow', editIndex);
//				alert(data.msg);				
//			}
//		});
		$('#dg').datagrid('cancelEdit', editIndex)
		.datagrid('deleteRow', editIndex);
	}

	function accept(){
		if (endEditing()){
			$('#dg').datagrid('acceptChanges');
			editIndex = undefined;
		}
	}

	function reject(){
		$('#dg').datagrid('rejectChanges');
		editIndex = undefined;
	}

	function getChanges(){
		var rows = $('#dg').datagrid('getChanges');
		alert(rows.length+' rows are changed!');
	}

});